// 引入https模块
const https = require('https')
const cheerio = require("cheerio");
const axios = require("axios");
// 引入fs模块
const fs = require('fs')
const url = 'https://weibo.com/ajax/side/hotSearch'
module.exports = {
    wbnews_spider: async (ctx, next) => {
        // 伪造请求头，使用请求方法
        const req = https.request(url, {
            method: 'get', header: {
                'authority': 'weibo.com',
                'method': 'GET',
                'path': '/ajax/side/hotSearch',
                'scheme': 'https',
                'accept': 'application/json, text/plain',
                'accept-encoding': 'gzip, deflate',
                'accept-language': 'zh-CN,zh;q=0.9',
                'client-version': 'v2.39.11',
                'cookie: SUB': '_2AkMV8Va5f8NxqwJRmfsRy2zibYp3zg3EieKjradiJRMxHRl-yT9jqm4TtRB6PnF4VrUm_8R2sMVNbWPxXv32RSyaeK0g; UOR=www.fengfengzhidao.com,s.weibo.com,www.fengfengzhidao.com; SINAGLOBAL=8261570728013.232.1676544993660; ULV=1676544993765:1:1:1:8261570728013.232.1676544993660:; XSRF-TOKEN=qkag5L4a3VKkyOta0XWU4VbU; WBPSESS=mm07v0uQ8nV44TNSi6a9Lebp7dgHcnVYLsUiAsmisBaK6r-N6miZrPKTfFk95sDm5C-tGfyzcpx72fceiJuVVoH9bb6f5m14tS_CjUDpn3Ql_R3fAgQx0jcjKGNCMymJe0upWL2bHmxhlSK5QbBFvGKlwip_tdk8fAU3AUvnDdk=',
                'referer': 'https://weibo.com/newlogin?tabtype=search&gid=&openLoginLayer=0&url=',
            }
        }, res => {
            let chunks = []
            // 监听网页数据
            res.on('data', chunk => {
                chunks.push(chunk)
            })
            // 监听网页数据完成
            res.on('end', () => {
                // 接收爬取的数据进行字符串转化
                const result = Buffer.concat(chunks).toString('utf-8')
                // 字符串转换为对象
                let hotArr = JSON.parse(result).data.realtime
                // 定义一个新数组，用于存放筛选后的数据
                let newHotArr = []
                // 遍历数据，指定自己需要的数据
                hotArr.forEach(item => {
                    let newNum = parseInt(item.num / 10000)
                    newHotArr.push({
                        'note': item.note,
                        'rank': item.rank,
                        'num': newNum,
                        'url': 'https://s.weibo.com/weibo?q=' + item.note
                    })
                })
                // 使用fs模块，将数据写入文件中
                fs.writeFile('src/static/wbnews/wb_news.js', JSON.stringify(newHotArr), function (err) {
                    if (err) {
                        console.log('文件写入失败:' + err.message);
                    } else {
                        console.log('文件写入成功');
                    }
                })
            })
        })
        // 发送请求
        req.end()
        async function readFile(path) {
            return new Promise((resolve, reject) => {
                fs.readFile(path, (err, data) => {
                    if (err) { throw err }
                    if (data) {
                        resolve(data)
                    } else {
                        resolve(false)
                    }
                })
            })
        }
        const res = await readFile('src/static/wbnews/wb_news.js')
        let newsStr = res.toString()
        let newsArr = JSON.parse(newsStr)
        ctx.body = {
            code: 1000,
            msg: '获取新闻成功',
            news: newsArr
        }
    },
    zhnews_spider: async (ctx, next) => {
        
        axios.get(`https://www.zhihu.com/billboard`).then((response) => {
            let $ = cheerio.load(response.data);

            var hotList = []

            $("a.HotList-item").each((index, element) => {
                var entry = {}
                entry["rank"] = $(element).find(".HotList-itemIndex").text();
                entry["title"] = $(element).find(".HotList-itemTitle").text();
                entry["hotNum"] = $(element).find(".HotList-itemMetrics").text();
                entry["src"] = $(element).find("HotList-img").text();
                var reg = /<img src="(.*)" alt=/;
                var picture_url = reg.exec(String($(element).html()));
                if (picture_url != null) {
                    entry["picture_url"] = picture_url[1].trim();
                } else {
                    entry["picture_url"] = "null";
                }
                hotList.push(entry);
            });


            fs.writeFile('src/static/zhnews/zh_news.json', JSON.stringify(hotList, null, "\t"), (err) => {
                if (err == null) {
                    console.log("知乎热搜爬取成功");
                } else {
                    console.log(err);
                }
            });

        });
        async function readFile(path) {
            return new Promise((resolve, reject) => {
                fs.readFile(path, (err, data) => {
                    if (err) { throw err }
                    if (data) {
                        resolve(data)
                    } else {
                        resolve(false)
                    }
                })
            })
        }
        const res = await readFile('src/static/zhnews/zh_news.json')
        let newsStr = res.toString()
        let newsArr = JSON.parse(newsStr)
        ctx.body = {
            code: 1000,
            msg: '获取新闻成功',
            news: newsArr
        }
    }
}